package GUI;
import java.util.ArrayList;

import processing.core.PImage;



public class TextBox
{
	public int x;
	public int y;
	public int width = 32;
	public String text;
	public ScrollBar scrollBar;
	public boolean active;
	public ArrayList<Control.Character> charactersToShow;
	public ArrayList<Control.CatchPhrase> catchPhraseToShow;

	////////////////////////////////////////////////////////////////////////////////////////////////////
	public TextBox()
	{
		text = "";
		scrollBar = new ScrollBar();
		active=false;
		charactersToShow = Utils.characters;
		catchPhraseToShow = Utils.catchphrases;
	}

	public void draw()
	{
		Utils.globalProcessing.rectMode(Utils.globalProcessing.CORNER);
		
		Utils.globalProcessing.noFill();
		//Utils.globalProcessing.fill(200);
		Utils.globalProcessing.stroke(80);
		Utils.globalProcessing.strokeWeight(2);
		Utils.globalProcessing.rect(x, y, width, 30);

		Utils.globalProcessing.fill(Utils.textColor);	
		//Utils.globalProcessing.textFont(Utils.globalProcessing.plotFont, 14);
		Utils.globalProcessing.textAlign(Utils.globalProcessing.LEFT);
		Utils.globalProcessing.text(text, x + 5, y + 20);

		if(active){
			Utils.globalProcessing.fill(44);
			Utils.globalProcessing.stroke(80);
			Utils.globalProcessing.strokeWeight(1);
			Utils.globalProcessing.rect(x, y+30, width, 130);


			scrollBar.width = 15;
			scrollBar.x =  x +width-15;
			scrollBar.y = y+30;
			scrollBar.height = 120;
			scrollBar.draw();  
			scrollBar.size = 1/(float)(Utils.characters.size()-3);

			if(label.equals("Names"))
				showCharacterButtons();
			else if(label.equals("CatchPhrases"))
				showCatchPhrasesButtons();
		}

	}

	public void keyTyped()
	{
		if(Utils.globalProcessing.key==Utils.globalProcessing.ENTER){
			if(label.equals("Names")){
				if(charactersToShow.size()>0){
					Control.Character c =charactersToShow.get(0);
					if(Utils.characterSelected.contains(c)){
						Utils.ccip.removeCharacterSelected(c);
					}
					else{
						Utils.ccip.addCharacterSelected(c);
					}
					Utils.characterTextBox.active=false;
					Utils.characterTextBox.text = c.getShortName();
					text = new String();
				}
			}
			else if(label.equals("CatchPhrases")){
				if(catchPhraseToShow.size()>0){
				Control.CatchPhrase c = catchPhraseToShow.get(0);
				
				
				
				
				
				if(Utils.catchPhrasesSelected.contains(c)){
					//Utils.catchPhrasesSelected.remove(c);
					//change catch phrase to not selected or selected
					int i;
					for(i =0;i<Utils.ccip.cd.size();i++){
						if(Utils.ccip.cd.get(i).characterSelectedButtonPanel.c.getName().equals(c.getCharacter().getName())){
							for(int j=0;j<Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.size();j++){
								if(Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.get(j).label.equals(c.getPhrase())){
									if(Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.get(j).selected==true){
										Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.get(j).selected=false;
										Utils.catchPhrasesSelected.remove(Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.get(j).catchPhrase);
									}
									else{
										System.out.println("Error mismatch catchPhrasesSelected");
									}
								break;
								}
							}
						}
						
				}
				}
				else{ //if not contained
					Utils.ccip.addCharacterSelected(c.getCharacter());
					Utils.catchPhrasesSelected.add(c);
					int i;
					for(i=0;i<Utils.ccip.cd.size();i++){
						if(Utils.ccip.cd.get(i).characterSelectedButtonPanel.c.getName().equals(c.getCharacter().getName())){
							for(int j=0;j<Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.size();j++)
								if(Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.get(j).label.equals(c.getPhrase())){
									Utils.ccip.removeOtherCatchPhrases(Utils.catchPhraseSuggestionButtons.get(0));
									Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.get(j).selected=true;
								}
								break;
						}
						}
				}
				Utils.catchPhrasesTextBox.active=false;
				Utils.catchPhrasesTextBox.text = c.getPhrase();
				text = new String();

				
				
				
				}
        		Utils.graphArea.createLineGraphPoints();

			}
		}
		else if(Utils.globalProcessing.key == 8)
		{
			if(text.length() > 0) text = text.substring(0, text.length() - 1);
		}
		else if(text.length()>16){
			text = text.substring(0, text.length() - 1);
			text += Utils.globalProcessing.key;
		}
		else
		{
			text += Utils.globalProcessing.key;
		}
		if(label.equals("Names"))
			updateCharacters();
		else if(label.equals("CatchPhrases"))
			updateCatchPhrases();
			
	}

	

	public boolean mouseOver()
	{
		if(Utils.globalProcessing.mouseX > x && Utils.globalProcessing.mouseX < x + width && 
				Utils.globalProcessing.mouseY > y && Utils.globalProcessing.mouseY < y + 30) return true;
		return false;
	}

	public boolean mouseOverSuggestions(){

		if(Utils.globalProcessing.mouseX > x && Utils.globalProcessing.mouseX < x + width && 
				Utils.globalProcessing.mouseY > y+30 && Utils.globalProcessing.mouseY < y + 160) return true;

		return false;
	}

	//show  catch phrases buttons in the suggestions
	private void showCatchPhrasesButtons() {

		float j = Main.map (scrollBar.val,0,1,0,catchPhraseToShow.size()-3);
		Utils.catchPhraseSuggestionButtons = new ArrayList<CatchPhraseButton>();
		for(int i=0;i<catchPhraseToShow.size();i++){
				CatchPhraseButton  b = new CatchPhraseButton();
				b.setLabel(catchPhraseToShow.get(i).getPhrase());
				b.c = catchPhraseToShow.get(i);
				Utils.catchPhraseSuggestionButtons.add(b);
			
		}
		if(j<0)
			j=0;
		for(int i=(int)j;i<(int)j+5 && i<Utils.catchPhraseSuggestionButtons.size();i++){
			CatchPhraseButton b = Utils.catchPhraseSuggestionButtons.get(i);
			b.x=x;
			b.y=y+30+(i-(int)j)*25;
			b.draw();
		}	
	}
	//show  characters buttons in the suggestions
	private void showCharacterButtons() {

		float j = Main.map (scrollBar.val,0,1,0,charactersToShow.size()-3);
		Utils.characterSuggestionButtons= new ArrayList<CharacterButton>();
		for(int i=0;i<charactersToShow.size();i++){
			CharacterButton b = new CharacterButton();
			b.setCharacter(charactersToShow.get(i));
			b.setLabel(charactersToShow.get(i).getShortName());
			Utils.characterSuggestionButtons.add(b);
		}
		if(j<0)
			j=0;
		for(int i=(int)j;i<(int)j+5 && i<Utils.characterSuggestionButtons.size();i++){
			CharacterButton b = Utils.characterSuggestionButtons.get(i);
			b.x=x;
			b.y=y+30+(i-(int)j)*25;
			b.draw();
		}		
	}


	public void updateCharacters(){
		charactersToShow = new ArrayList<Control.Character>();
		for(int i=0;i<Utils.characters.size();i++){
			if(Utils.characters.get(i).getShortName().toLowerCase().startsWith(text.toLowerCase())){
				charactersToShow.add(Utils.characters.get(i));
				//System.out.println("I add "+Utils.characters.get(i).getName());
			}
		}
	}


	private void updateCatchPhrases() {
		catchPhraseToShow = new ArrayList<Control.CatchPhrase>();
		for(int i=0;i<Utils.catchphrases.size();i++){
			if(Utils.catchphrases.get(i).getPhrase().toLowerCase().contains(text.toLowerCase())){
				catchPhraseToShow.add(Utils.catchphrases.get(i));
			}
		}
	}


	////////////////////////////////////////////////////////////////////////////////////////////////////
	PImage icon;
	String label;
}